import java.util.Stack;

public class Sort {
    public static void insertSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int end = i;
            int tmp = array[i + 1];
            while (end >= 0) {
                if (array[end] > tmp) {
                    array[end + 1] = array[end];
                    end--;
                } else {
                    break;
                }
            }
            array[end + 1] = tmp;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap = gap / 3 + 1;
            for (int i = 0; i < array.length - gap; i++) {
                int end = i;
                int tmp = array[end + gap];
                while (end >= 0) {
                    if (array[end] > tmp) {
                        array[end + gap] = array[end];
                        end -= gap;
                    } else {
                        break;
                    }
                }
                array[end + gap] = tmp;
            }
        }
    }

    private static void swap(int[] array, int index1, int index2) {
        int tmp = array[index1];
        array[index1] = array[index2];
        array[index2] = tmp;
    }

    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean exchange = false;
            for (int j = 1; j < array.length; j++) {
                if (array[j] < array[j - 1]) {
                    swap(array, j, j - 1);
                    exchange = true;
                }
            }
            if (!exchange) {
                break;
            }
        }
    }

    public static void selectSort(int[] array) {
        int begin = 0;
        int end = array.length - 1;
        while (begin < end) {
            int maxi = begin;
            int mini = begin;
            for (int i = begin; i <= end; i++) {
                if (array[i] > array[maxi]) {
                    maxi = i;
                }
                if (array[i] < array[mini]) {
                    mini = i;
                }
            }
            swap(array, begin, mini);
            if (begin == maxi) {
                maxi = mini;
            }
            swap(array, end, maxi);
            begin++;
            end--;
        }
    }

    private static int getMidIndex(int[] array, int left, int right) {
        int mid = (left + right) / 2;
        if (array[left] < array[mid]) {
            if (array[mid] < array[right])
                return mid;
            else if (array[left] < array[right])
                return right;
            else
                return left;
        } else {// array[left] > array[mid]
            if (array[mid] > array[right])
                return mid;
            else if (array[left] > array[right])
                return right;
            else
                return left;
        }
    }

    private static int partSort1(int[] array, int left, int right) {
        int midi = getMidIndex(array, left, right);
        swap(array, midi, left);

        int keyi = left;
        while (left < right) {
            while (left < right && array[right] >= array[keyi]) {
                right--;
            }
            while (left < right && array[left] <= array[keyi]) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, keyi);
        return left;
    }

    private static int partSort2(int[] array, int left, int right) {
        int midi = getMidIndex(array, left, right);
        swap(array, midi, left);

        int key = array[left];
        int hole = left;
        while (left < right) {
            while (left < right && array[right] >= key) {
                right--;
            }
            array[hole] = array[right];
            hole = right;
            while (left < right && array[left] <= key) {
                left++;
            }
            array[hole] = array[left];
            hole = left;
        }
        array[hole] = key;
        return hole;
    }

    private static int partSort3(int[] array, int left, int right) {
        int midi = getMidIndex(array, left, right);
        swap(array, midi, left);

        int prev = left;
        int cur = left + 1;
        int keyi = prev;
        while (cur <= right) {
            if (array[cur] < array[keyi]) {
                prev++;
                swap(array, prev, cur);
            }
            cur++;
        }
        swap(array, prev, keyi);
        return prev;
    }

    private static void _quickSort(int[] array, int begin, int end) {
        if (begin >= end)
            return;
        int key = partSort3(array, begin, end);
        _quickSort(array, begin, key - 1);
        _quickSort(array, key + 1, end);
    }

    public static void quickSort(int[] array) {
        _quickSort(array, 0, array.length - 1);//保持接口一致性
    }

    private static void _quickSort_3Line(int[] array, int begin, int end) {
        if (begin >= end)
            return;
        int left = begin;
        int right = end;
        int cur = left + 1;
        int midi = getMidIndex(array, left, right);
        swap(array, midi, left);
        int key = array[left];
        while (cur <= right) {
            if (array[cur] < key) {
                swap(array, cur, left);
                cur++;
                left++;
            } else if (array[cur] > key) {
                swap(array, cur, right);
                right--;
            } else {
                cur++;
            }
        }
        _quickSort_3Line(array, begin, left - 1);
        _quickSort_3Line(array, right + 1, end);
    }

    public static void quickSort_3Line(int[] array) {
        _quickSort_3Line(array, 0, array.length - 1);
    }

    public static void quickSort_NonR(int[] array) {
        Stack<Integer> stack = new Stack<>();
        stack.push(array.length - 1);
        stack.push(0);
        while (!stack.empty()) {
            int left = stack.peek();
            stack.pop();
            int right = stack.peek();
            stack.pop();
            int keyi = partSort3(array, left, right);
            if (keyi + 1 > right) {
                stack.push(right);
                stack.push(keyi + 1);
            }
            if (left < keyi - 1) {
                stack.push(keyi - 1);
                stack.push(left);
            }
        }
    }

    private static void _mergeSort(int[] array, int left, int right) {
        if (left == right) {
            return;
        }
        if (right - left + 1 < 10) {
            insertSort(array);
            return;
        }
        //归
        int mid = (left + right) / 2;
        _mergeSort(array, left, mid);
        _mergeSort(array, mid + 1, right);
        //并
        int begin1 = left;
        int begin2 = mid + 1;
        int[] tmpArray = new int[right - left + 1];
        int i = 0;
        while (begin1 <= mid && begin2 <= right) {
            if (array[begin1] < array[begin2]) {
                tmpArray[i++] = array[begin1++];
            } else {
                tmpArray[i++] = array[begin2++];
            }
        }
        while (begin1 <= mid) {
            tmpArray[i++] = array[begin1++];
        }
        while (begin2 <= right) {
            tmpArray[i++] = array[begin2++];
        }
        System.arraycopy(tmpArray, 0, array, left, tmpArray.length);
    }

    public static void mergeSort(int[] array) {
        _mergeSort(array, 0, array.length - 1);
    }

    public static void mergeSort_NonR(int[] array) {
        int[] tmpArray = new int[array.length];
        int gap = 1;
        while (gap < array.length) {
            for (int i = 0; i < array.length; i += 2 * gap) {
                int begin1 = i;
                int end1 = begin1 + gap - 1;
                int begin2 = end1 + 1;
                int end2 = begin2 + gap - 1;
                if (end1 >= array.length || begin2 >= array.length) {
                    break;
                }
                if (end2 >= array.length) {
                    end2 = array.length - 1;
                }
                int j = begin1;
                while (begin1 <= end1 && begin2 <= end2) {
                    if (array[begin1] < array[begin2]) {
                        tmpArray[j++] = array[begin1++];
                    } else {
                        tmpArray[j++] = array[begin2++];
                    }
                }
                while (begin1 <= end1) {
                    tmpArray[j++] = array[begin1++];
                }
                while (begin2 <= end2) {
                    tmpArray[j++] = array[begin2++];
                }
                System.arraycopy(tmpArray, i, array, i, end2 - i + 1);
            }
            gap *= 2;
        }
    }

    private static void adjustDown(int[] array, int size, int parent) {
        int child = parent * 2 + 1;
        while (child < size) {
            if (child + 1 < size && array[child] < array[child + 1]) {
                child++;
            }
            if (array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }
        }
    }

    public static void heapSort(int[] array) {
        for (int i = (array.length - 1 - 1) / 2; i >= 0; i--) {
            adjustDown(array, array.length, i);
        }
        for (int i = array.length - 1; i > 0; i--) {
            swap(array, 0, i);
            adjustDown(array, i, 0);
        }
    }
}
